package com.ruoyi.quartz.task;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.domain.TlDyGoods;
import com.ruoyi.system.mapper.TlDyGoodsMapper;
import com.ruoyi.system.service.EsDyGoodsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.ruoyi.common.utils.StringUtils;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask
{
    @Value("${ruoyi.dyAppid}")
    private String  dyAppid;
    @Value("${ruoyi.dySecretKey}")
    private String  dySecretKey;

    @Autowired
    private TlDyGoodsMapper tlDyGoodsMapper;
    @Autowired
    private EsDyGoodsRepository esDyGoodsRepository;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    /**
     * 抖音商品同步
     * @param params 更新日期
     */
    public void ryParams(String params)
    {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        if(StringUtils.isBlank(params)){
            params = simpleDateFormat.format(new Date());
        }
        //获取全部数据
        Integer page = 1;
        while (true){
            String get = HttpUtils.sendGet("http://api.zcxq.cn/open/goods/list/", "app_id=" + dyAppid + "&sec=" + dySecretKey + "&page="+page+"&uptime="+params);
            JSONObject jsonObject =  JSONObject.parseObject(get);
            String code = jsonObject.getString("code");
            if(!code.equals("200")){
                return ;
            }
            String data = jsonObject.getString("data");
            //开始解析
            List<TlDyGoods> insertList = new ArrayList<>();
            List<TlDyGoods> updateList = new ArrayList<>();
            JSONObject parse1 = JSONObject.parse(data);
            Integer total = parse1.getInteger("total");
            if(total==0){
                return ;
            }
            String list = parse1.getString("list");
            JSONArray parse = JSONArray.parse(list);
            if(parse.size()==0){
                return;
            }
            //oldids
            List<Long> oldIds = new ArrayList<>();
            for(int i=0;i<parse.size();i++){
                Object o = parse.get(i);
                TlDyGoods tlDyGoods = JSONObject.parseObject(o.toString(), TlDyGoods.class);
                oldIds.add(tlDyGoods.getId());
            }
            List<TlDyGoods> oldGoods = new ArrayList<>();
            if(oldIds.size()>0){
                oldGoods = tlDyGoodsMapper.selectTlDyGoodsByIds(oldIds);
            }
            Map<Long, List<TlDyGoods>> collect = new HashMap<>();
            if(oldGoods!=null && oldGoods.size()>0){
                collect = oldGoods.stream().collect(Collectors.groupingBy(TlDyGoods::getId));
            }
            for(int i=0;i<parse.size();i++){
                Object o = parse.get(i);
                TlDyGoods tlDyGoods = JSONObject.parseObject(o.toString(), TlDyGoods.class);
                List<TlDyGoods> tlDyGoods1 = new ArrayList<>();
                if(collect!=null && collect.size()>0){
                    tlDyGoods1   = collect.get(tlDyGoods.getId());
                }
                if(tlDyGoods1!=null && tlDyGoods1.size()>0){
                    Long upTime = Long.parseLong(tlDyGoods1.get(0).getUpTime());
                    Long upTime1 = Long.parseLong(tlDyGoods.getUpTime());
                    if(upTime<=upTime1){
                        //需要更新的列表
                        updateList.add(tlDyGoods);
                    }
                }else {
                    insertList.add(tlDyGoods);
                }
            }
            if(insertList.size()>0){
                tlDyGoodsMapper.insertTlDyGoodsList(insertList);
                esDyGoodsRepository.saveAll(insertList);
            }
            if(updateList.size()>0){
                for (TlDyGoods update:updateList) {
                    tlDyGoodsMapper.updateTlDyGoods(update);
                }
                esDyGoodsRepository.saveAll(updateList);
            }
            page++;
        }
    }

    public void ryNoParams()
    {
        System.out.println("执行无参方法");
    }
}
